package com.otod.sys.operater.log.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.entity.Page;
import com.otod.commons.web.BaseController;
import com.otod.sys.operater.log.bo.LogRecordBo;
import com.otod.sys.operater.log.service.IOperaterLogService;
import com.otod.sys.vo.AsmgOperaterLogVo;

@Controller
@RequestMapping("sys/log")
public class AsmgOperaterLogController extends BaseController {
	@Autowired
	private IOperaterLogService iOperaterLogService;
	
	/**
	 * 新增日志
	 * @param asmgOperaterLogVo
	 * @return
	 */
	@RequestMapping(value="addLog",method = RequestMethod.GET)
	@ResponseBody
	public String addLog(AsmgOperaterLogVo asmgOperaterLogVo ){
		iOperaterLogService.addLog(asmgOperaterLogVo);
		return JsonSuccess.factory("新增日志成功");
	}
	/**
	 * 删除日志
	 * @param startTime 删除日志开始时间
	 * @param endTime 删除日志结束时间
	 * @return
	 */
	@RequestMapping(value = "deleteLogTime",method = RequestMethod.GET)
	@ResponseBody
	public String deleteLogTime(Date startTime,Date endTime){
		iOperaterLogService.deleteLogTime(startTime, endTime);
		return JsonSuccess.factory("删除日志成功");
	}
	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryPage",method = RequestMethod.GET)
	@ResponseBody
	public Page queryPage(HttpServletRequest request,Date startTime,Date endTime){
		Map map = super.getParamMapFromRequest(request);
		Page page = new Page(request);
		
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return iOperaterLogService.queryPge(page, map);
	}
	
	
	/**
	 * 查询历史记录
	 * @param taskId
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value = "queryRecord",method = RequestMethod.GET)
	@ResponseBody
	public List<LogRecordBo> queryRecord(String busiId){
		List<LogRecordBo> list = Lists.newArrayList();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("busiId", busiId);
		List<AsmgOperaterLogVo> asmgOperaterLogVos = iOperaterLogService.queryRecord(map);
		List<LogRecordBo> bos = Lists.newArrayList();
		LogRecordBo bo = null;
		for (AsmgOperaterLogVo asmgOperaterLogVo : asmgOperaterLogVos) {
	           JSONArray jsonArray;
				try {					
					jsonArray = JSONArray.parseArray(asmgOperaterLogVo.getDepict());
					JSONObject jsonObject;
					for (int i = 0; i < jsonArray.size(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
		                bo = new LogRecordBo();
		                bo.setEmpNo(asmgOperaterLogVo.getEmpName());
		                bo.setLogTm(asmgOperaterLogVo.getLogTm());
		                bo.setOperaterType(asmgOperaterLogVo.getOperaterType());
		                bo.setName(jsonObject.get("name").toString());
		                bo.setNewVal(jsonObject.get("nVal").toString());
		                bos.add(bo);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					//throw new AppException("json字符串解析失败");
				}	         
		}		
		
	
		return bos;
	}
	
	/**
	 * 查询历史记录 分页 主要是查看项目 历史记录 其他使用 会报错
	 * @param taskId
	 * @return
	 * @throws AppException 
	 */
	@RequestMapping(value = "queryRecordPage",method = RequestMethod.GET)
	@ResponseBody
	public Page queryRecordPage(HttpServletRequest request,String busiId){
		Page page = new Page(request);
		if(StringUtils.isEmpty(busiId)){
			return page;
		}
		List<LogRecordBo> list = Lists.newArrayList();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("busiId", busiId);
		page = iOperaterLogService.queryPge(page, map);
		List<AsmgOperaterLogVo> asmgOperaterLogVos = page.getAaData();
		List<LogRecordBo> bos = Lists.newArrayList();
		LogRecordBo bo = null;
		for (AsmgOperaterLogVo asmgOperaterLogVo : asmgOperaterLogVos) {
	           JSONArray jsonArray = new JSONArray();
				try {	
					jsonArray = JSONArray.parseArray(asmgOperaterLogVo.getDepict());
					JSONObject jsonObject;
					for (int i = 0; i < jsonArray.size(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
		                bo = new LogRecordBo();
		                bo.setEmpNo(asmgOperaterLogVo.getEmpNo());
		                bo.setEmpName(asmgOperaterLogVo.getEmpName());
		                bo.setLogTm(asmgOperaterLogVo.getLogTm());
		                bo.setOperaterType(asmgOperaterLogVo.getOperaterType());
		                bo.setName(jsonObject.get("key").toString());
		                bo.setNewVal(jsonObject.get("nVal").toString());
		                bo.setOldVal(jsonObject.get("oVal").toString());
		                bos.add(bo);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					//throw new AppException("json字符串解析失败");
				}	         
		}		
		
		page.setAaData(bos);
		return page;
	}
	
}
